package com.auth.manage.user.domain.facade;

import com.auth.manage.base.responce.BaseResponce;
import com.auth.manage.base.responce.MultipleResponce;
import com.auth.manage.base.responce.SingleResponce;
import com.auth.manage.common.user.api.request.UserRegisterRequest;
import com.auth.manage.common.user.api.request.condition.UserQueryCondition;
import com.auth.manage.common.user.api.responce.UserResponce;
import com.auth.manage.common.user.api.service.UserFacadeService;
import com.auth.manage.user.domain.entity.User;
import com.auth.manage.user.domain.entity.convertor.UserConvertor;
import com.auth.manage.user.domain.service.UserService;
import com.auth.manage.user.domain.service.query.UserQueryFactory;
import com.auth.manage.user.domain.service.query.strategy.UserQueryStrategy;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.Collection;

@RequiredArgsConstructor
@DubboService(version = "1.0.0")  // 暴露为一个可以被远程调用的接口，该接口去调用Service的代码
public class UserRpcServiceImpl implements UserFacadeService {
    private final UserQueryFactory factory;
    private final UserService userService;


    // 这个代码要做到和其他地方解耦
    // 当前实现类是基于api的UserFacade接口实现的，因此UserQueryCondition需要在api模块定义,而UserQueryCondition内部封装了UserQueryStrategyCode，所以UserQueryStrategyCode也需要在api模块中定义
    @Override
    public MultipleResponce<UserResponce> queryByCondition(UserQueryCondition condition) {
        // 1.获取策略
        UserQueryStrategy queryStrategy = factory.getQueryStrategy(condition.userQueryStrategyCode().name());  // 根据策略名获取具体的实现类
        // 2.执行方法
        Collection<User> res = queryStrategy.query(condition);  // 具体的实现类去执行该方法
        // 3.封装结果返回
        Collection<UserResponce> userResponces = UserConvertor.INSTANCE.do2VO(res);
        return MultipleResponce.of(userResponces);
    }

    @Override
    public SingleResponce<UserResponce> register(UserRegisterRequest request) {
        return userService.register(request.getTelephone(), request.getInviteCode());
    }

    // 冻结用户
    @Override
    public BaseResponce freezeUserByUserId(Long userId) {
        userService.freezeUserByUserId(userId);
        return BaseResponce.success();
    }

    @Override
    public BaseResponce unfreezeUserByUserId(Long userId) {
        userService.unfreezeUserByUserId(userId);
        return BaseResponce.success();
    }
}